




var _oMessages =
{
sCloseAlert:	LOCID_FORMS_SAVE_CONFIRM_TITLE

};






var _oFormats =
{
sNone:		"",
sEmailBody:	"emailbody",
sTimeZone:	"timezone"
}

var _oTypes =
{
sString:		"nvarchar",
sDateTime:		"datetime",
sBoolean:		"bit",
sMoney:			"money",
sInteger:		"int",
sFloat:			"float",
sPicklist:		"picklist",
sStatus:		"status",
sMemo:			"ntext",
sDecimal:		"decimal",
sLookup:		"lookup",
sOwner:			"owner",
sCustomer:		"customer",
sPrimaryKey:	"primarykey"
};

var _oConst =
{
sInvalidSchemaNameChars:		"[^A-Za-z0-9_]",

sCreateAttributeUrlFormat:		"manageAttribute.aspx?entityId={0}",
sUpdateAttributeUrlFormat:		"manageAttribute.aspx?attributeId={0}&entityId={1}",
sInformationPageId: 			"divInformation"
};






var _bSaving = false;
var _sInputXml = "";
var _iOldPrecision = null;




var _aStates = new Array;
var _iCurrentState = -1;
var _iNextStatusValue;
var _iNextStatusValueOriginal;












function ManageAttributeJsWindowOnLoad()
{
if (!_bCreate)
{
window.focus();
}


loadStates();


initControls();

_sInputXml = getAttributeXml();

loadPage(_oConst.sInformationPageId);

if (!txtDisplayName.Disabled)
{
txtDisplayName.SetFocus();
}

_iOldPrecision = Number(selMoneyPrecision.DataValue);
}

window.attachEvent('onload', ManageAttributeJsWindowOnLoad);




function document.onkeydown()
{
return ProcessKeyDown();
}

function _save()
{
saveAttributeAction(false, false);
}

function _saveandclose()
{
saveAttributeAction(true, false);
}

function _saveandnew()
{
saveAttributeAction(false, true);
}




window.attachEvent("onbeforeunload", window_onbeforeunload);
function window_onbeforeunload()
{
if (!_bSaving && isFormDirty())
{
event.returnValue = _oMessages.sCloseAlert;
}
}




function initControls()
{

onTypeChange();


onMemoFormatChange();
onIntFormatChange();
onStringFormatChange();


onStateChange();
}




function loadStates()
{
var oStatesXmlDoc = loadXmlDocument(divStatesXml.innerText);
var oStatesNode = oStatesXmlDoc.documentElement;
var iStateNode, oStateNode, oStateNodeList;
var iState, sStatuses;

if (oStatesNode != null)
{
_iNextStatusValue = Number(oStatesNode.attributes.getNamedItem("nextstatus").value);
_iNextStatusValueOriginal = _iNextStatusValue;

oStateNodeList = oStatesNode.selectNodes("state");
for (iStateNode = 0; iStateNode < oStateNodeList.length; iStateNode++)
{
oStateNode = oStateNodeList.item(iStateNode);
iState = Number(oStateNode.attributes.getNamedItem("value").value);
sStatuses = oStateNode.selectSingleNode("values").xml;
_aStates[iState] = sStatuses;
}
}
}










function CallbackParams(bClose, bReload, bRefreshGrid, bNew)
{
this.bClose			= bClose;
this.bReload		= bReload;
this.bRefreshGrid	= bRefreshGrid;
this.bNew			= bNew;
}

function remoteAttributeCommandCallback(oResult, oCallbackParams)
{
if (oResult.Success)
{

if (oResult.RemoteCommand.Command == _oAttrUtlConst.sCreateAttributeCommand)
{
_sAttributeId = oResult.ReturnValue;
}
else if (oResult.RemoteCommand.Command == _oAttrUtlConst.sDeleteAttributeCommand)
{
if (!IsNull(oResult.ReturnValue) && oResult.ReturnValue.Used)
{
showUsage(_oSCUConst.sAttributeMode, oResult.ReturnValue);
return true;
}
}


if (oCallbackParams.bRefreshGrid)
{
try
{
window.opener.gridAttributes.Refresh();
}
catch(e)
{}
}


if (oCallbackParams.bClose)
{
window.close();
return false;
}


if (oCallbackParams.bReload)
{

frmReload.action = formatString(_oConst.sUpdateAttributeUrlFormat, _sAttributeId, _sEntityId);
frmReload.submit();
return false;
}


if (oCallbackParams.bNew)
{
frmReload.action = formatString(_oConst.sCreateAttributeUrlFormat, _sEntityId);
frmReload.submit();
return false;
}
}

_bSaving = false;
return true;
}




function saveAttributeAction(bClose, bNew)
{
if (!_bSaving && validateParameters())
{
_bSaving = true;

var sDataXml = getAttributeXml();

var oCallbackParams = new CallbackParams(bClose, !bNew, true, bNew);

if (_bCreate)
{
createAttribute(sDataXml, remoteAttributeCommandCallback, oCallbackParams);
}
else if (isFormDirty())
{
updateAttribute(sDataXml, remoteAttributeCommandCallback, oCallbackParams);
}
else if (bClose)
{
window.close();
return;
}
else if (bNew)
{
frmReload.action = formatString(_oConst.sCreateAttributeUrlFormat, _sEntityId);
frmReload.submit();
return;
}
else
{
_bSaving = false;
}
}
}




function deleteAttributeAction()
{
var oCallbackParams = new CallbackParams(true, false, true, false);
deleteAttribute(_sAttributeId, true, remoteAttributeCommandCallback, oCallbackParams);
}




var _oVisiblePage = null;
function loadPage(sPageId)
{
var oPage = document.all(sPageId);


if (_oVisiblePage != null)
{
_oVisiblePage.style.display = "none";
}


_oVisiblePage = oPage;
_oVisiblePage.style.display = "inline";


if (sPageId != _oConst.sInformationPageId)
{
var oIFrameElement	= oPage.children(0);
var oIFrame			= document.frames(oIFrameElement.id);
if (oIFrame.location.href.indexOf("/_root/Blank.aspx") > -1)
{
oIFrame.location = oIFrameElement.url;
}
}
}










function getAttributeXml()
{
var oDataXml = createXmlDoc(_oTags.attribute);

addTextXmlNode(oDataXml, _oTags.entityid, _sEntityId);
addTextXmlNode(oDataXml, _oTags.attributeid, _sAttributeId);

addSchemaXml(oDataXml);
addTypeXml(oDataXml);

return convertXmlDocToString(oDataXml);
}




function addSchemaXml(oDataXml)
{
addControlDataValue(oDataXml, _oTags.displayname, txtDisplayName);
addControlDataValue(oDataXml, _oTags.schemaname,  txtSchemaName);
addControlDataValue(oDataXml, _oTags.reqlevel,	  selReqLevel);
addControlDataValue(oDataXml, _oTags.imemode,	  selIMEMode);
addControlDataValue(oDataXml, _oTags.description, txtDescription);
addControlDataValue(oDataXml, _oTags.searchable, selectSearchable);
}




function addTypeXml(oDataXml)
{

var oTypeXml = addXmlNode(oDataXml, _oTags.type);


addControlDataValue(oTypeXml, _oTags.name, selType);


switch (selType.DataValue)
{
case _oTypes.sString:	addStringXml(oTypeXml);		break;
case _oTypes.sDateTime:	addDateTimeXml(oTypeXml);	break;
case _oTypes.sBoolean:	addBooleanXml(oTypeXml);	break;
case _oTypes.sMoney:	addMoneyXml(oTypeXml);		break;
case _oTypes.sInteger:	addIntegerXml(oTypeXml);	break;
case _oTypes.sFloat:	addFloatXml(oTypeXml);		break;
case _oTypes.sDecimal:	addDecimalXml(oTypeXml);	break;
case _oTypes.sPicklist:	addPicklistXml(oTypeXml);	break;
case _oTypes.sStatus:	addStatusXml(oTypeXml);	break;
case _oTypes.sMemo:		addMemoXml(oTypeXml);		break;
}
}




function addStringXml(oTypeXml)
{
addControlDataValue(oTypeXml, _oTags.format, selStringFormat);
addControlDataValue(oTypeXml, _oTags.length, numMaxStringLen);
if (document.getElementById("selStringPhonetic") != null)
{
addControlDataValue(oTypeXml, _oTags.yomiof, selStringPhonetic);
}
}




function addDateTimeXml(oTypeXml)
{
addControlDataValue(oTypeXml, _oTags.format, selDatetimeFormat);
}




function addBooleanXml(oTypeXml)
{
if (!ledtBooleanValues.Disabled)
{
addXmlString(oTypeXml, ledtBooleanValues.DataXml);
}
}




function addMoneyXml(oTypeXml)
{
addControlDataValue(oTypeXml, _oTags.precision, selMoneyPrecision);
addControlDataValue(oTypeXml, _oTags.minvalue,  numMinMoneyValue);
addControlDataValue(oTypeXml, _oTags.maxvalue,  numMaxMoneyValue);
}




function addIntegerXml(oTypeXml)
{
addControlDataValue(oTypeXml, _oTags.format, selIntFormat);

if (selIntFormat.DataValue == null)
{
addControlDataValue(oTypeXml, _oTags.minvalue, numMinIntValue);
addControlDataValue(oTypeXml, _oTags.maxvalue, numMaxIntValue);
}
}




function addFloatXml(oTypeXml)
{
addControlDataValue(oTypeXml, _oTags.precision, selFloatPrecision);
addControlDataValue(oTypeXml, _oTags.minvalue,  numMinFloatValue);
addControlDataValue(oTypeXml, _oTags.maxvalue,  numMaxFloatValue);
}




function addDecimalXml(oTypeXml)
{
addControlDataValue(oTypeXml, _oTags.precision, selDecimalPrecision);
addControlDataValue(oTypeXml, _oTags.minvalue,  numMinDecimalValue);
addControlDataValue(oTypeXml, _oTags.maxvalue,  numMaxDecimalValue);
}




function addPicklistXml(oTypeXml)
{
if (!ledtPicklistValues.Disabled)
{
addXmlString(oTypeXml, ledtPicklistValues.DataXml);
}
}




function addStatusXml(oTypeXml)
{










{

onStateChange();


var iState, sStatusXml;

if (ledtStatusValues.Disabled)
{
if (!IsNull(_iNextStatusValueOriginal))
{
_iNextStatusValue = _iNextStatusValueOriginal;
}
else
{
_iNextStatusValue = 0;
}
}

sStatusXml = formatString('<states nextstatus="{0}">', _iNextStatusValue);
for (iState = 0; iState < _aStates.length; iState++)
{
if (_aStates[iState] != undefined)
{
sStatusXml += formatString('<state value="{0}">{1}</state>', iState, _aStates[iState]);
}
}
sStatusXml += '</states>';


addXmlString(oTypeXml, sStatusXml);
}

}




function addMemoXml(oTypeXml)
{
if (selMemoFormat.DataValue != _oFormats.sEmailBody)
{
addControlDataValue(oTypeXml, _oTags.length, numMaxMemoLen);
}
addControlDataValue(oTypeXml, _oTags.format, selMemoFormat);
}










function validateParameters()
{
var bValid = true;


bValid = bValid && validateRequiredValue(txtDisplayName);


bValid = bValid && (txtSchemaName.Disabled || validateSchemaName(txtSchemaName));


bValid = bValid && validateRequiredValue(selReqLevel);


bValid = bValid && validateRequiredValue(selType);


if (bValid)
{
switch (selType.DataValue)
{
case _oTypes.sString:	bValid = bValid && validateStringParams();		break;
case _oTypes.sDateTime:	bValid = bValid && validateDateTimeParams();	break;
case _oTypes.sBoolean:	bValid = bValid && validateBooleanParams();		break;
case _oTypes.sMoney:	bValid = bValid && validateMoneyParams();		break;
case _oTypes.sInteger:	bValid = bValid && validateIntegerParams();		break;
case _oTypes.sFloat:	bValid = bValid && validateFloatParams();		break;
case _oTypes.sDecimal:	bValid = bValid && validateDecimalParams();		break;
case _oTypes.sPicklist:	bValid = bValid && validatePicklistParams();	break;
case _oTypes.sStatus:	bValid = bValid && validateStatusParams();		break;
case _oTypes.sMemo:		bValid = bValid && validateMemoParams();		break;
}
}

return bValid;
}




function validateStringParams()
{
return validateRequiredValue(selStringFormat) &&
validateRequiredValue(numMaxStringLen);
}




function validateDateTimeParams()
{
return validateRequiredValue(selDatetimeFormat);
}




function validateBooleanParams()
{
return true;
}




function validateMoneyParams()
{
return validateRequiredValue(selMoneyPrecision)	&&
validateRequiredValue(numMinMoneyValue)	&&
validateRequiredValue(numMaxMoneyValue)	&&
validateMinMaxValues(numMinMoneyValue, numMaxMoneyValue);
}




function validateIntegerParams()
{
return	selIntFormat.DataValue != null			||
validateRequiredValue(numMinIntValue)	&&
validateRequiredValue(numMaxIntValue)	&&
validateMinMaxValues(numMinIntValue, numMaxIntValue);
}




function validateFloatParams()
{
return validateRequiredValue(selFloatPrecision)	&&
validateRequiredValue(numMinFloatValue)	&&
validateRequiredValue(numMaxFloatValue)	&&
validateMinMaxValues(numMinFloatValue, numMaxFloatValue);
}




function validateDecimalParams()
{
return validateRequiredValue(selDecimalPrecision)	&&
validateRequiredValue(numMinDecimalValue)	&&
validateRequiredValue(numMaxDecimalValue)	&&
validateMinMaxValues(numMinDecimalValue, numMaxDecimalValue);
}




function validatePicklistParams()
{
return true;
}




function validateStatusParams()
{
if (ledtStatusValues.HasDefaultValue)
{
onStateChange();
for (iState = 0; iState < _aStates.length; iState++)
{
if (_aStates[iState] != undefined)
{
var oXml	= CreateXmlDocument(false);
oXml.loadXML(_aStates[iState]);
if (oXml.selectSingleNode("/values[@default]") == null)
{
alert (LOCID_ATTRUTL_EMPTYSTATUS);
return false;
}
}
}
}
return true;
}




function validateMemoParams()
{
var sFormat = selMemoFormat.DataValue;
return (sFormat == _oFormats.sEmailBody) || validateRequiredValue(numMaxMemoLen);
}





function validateMinMaxValues(numMinVal, numMaxVal)
{
var iMin = numMinVal.DataValue;
var iMax = numMaxVal.DataValue;

if (iMin > iMax)
{
alert(LOCID_MANATTR_REVERSEDMINMAX);
numMinVal.SetFocus();
return false;
}

return true;
}










function onDisplayNameChange()
{
if (_bCreate &&
(txtDisplayName.DataValue != null && txtDisplayName.DataValue.length > 0) &&
(txtSchemaName.DataValue == null || txtSchemaName.DataValue.length == 0))
{
var regExp = new RegExp(_oConst.sInvalidSchemaNameChars, "g");
txtSchemaName.DataValue = txtDisplayName.DataValue.replace(regExp, "").substr(0, txtSchemaName.maxLength);
}
}




var _oVisibleTypeParams = null;
function onTypeChange()
{
if ((selType.DataValue == _oTypes.sBoolean) || (selType.DataValue == _oTypes.sPicklist)
|| (selType.DataValue == _oTypes.sLookup) || (selType.DataValue == _oTypes.sOwner)
|| (selType.DataValue == _oTypes.sCustomer) || (selType.DataValue == _oTypes.sPrimaryKey)
|| (selType.DataValue == _oTypes.sStatus))
{
selIMEMode.selectedIndex = 0;
imeParams.style.display = "none";
}
else
{
imeParams.style.display = "block";
}


if (_oVisibleTypeParams != null)
{
_oVisibleTypeParams.style.display = "none";
}

_oVisibleTypeParams = document.all(selType.DataValue + "Params");

if (_oVisibleTypeParams != null)
{
_oVisibleTypeParams.style.display = "block";
}


if (selType.DataValue != _oTypes.sString)
{
if (document.getElementById("selStringPhonetic") != null)
{
selStringPhonetic.selectedIndex = 0;
}
PhoneticGuideParams.style.display = "none";
}
else
{
onStringFormatChange();
}
}




var _oVisibleMemoParams = null;
function onMemoFormatChange()
{
var sFormat = (selMemoFormat.DataValue == null) ? "" : selMemoFormat.DataValue;

hideFormatSpecificParams(_oVisibleMemoParams);
_oVisibleMemoParams = showFormatSpecificParams(sFormat, "MemoParams");
}




function onStringFormatChange()
{
var sFormat = (selStringFormat.DataValue == null) ? "" : selStringFormat.DataValue;

if(sFormat == "phoneticguide")
{
PhoneticGuideParams.style.display = "block";
}
else
{
if (document.getElementById("selStringPhonetic") != null)
{
selStringPhonetic.selectedIndex = 0;
}
PhoneticGuideParams.style.display = "none";
}
}




var _oVisibleIntParams = null;
function onIntFormatChange()
{
var sFormat = (selIntFormat.DataValue == null) ? _oFormats.sNone : selIntFormat.DataValue;

hideFormatSpecificParams(_oVisibleIntParams);
_oVisibleIntParams = showFormatSpecificParams(sFormat, "IntegerParams");
}




function onFloatPrecisionChange()
{
var iPrecision = Number(selFloatPrecision.DataValue);
numMinFloatValue.Precision = iPrecision;
numMaxFloatValue.Precision = iPrecision;
}




function onDecimalPrecisionChange()
{
var iPrecision = Number(selDecimalPrecision.DataValue);
numMinDecimalValue.Precision = iPrecision;
numMaxDecimalValue.Precision = iPrecision;
}




function onMoneyPrecisionChange()
{
var iPrecision = Number(selMoneyPrecision.DataValue);
numMinMoneyValue.Precision = iPrecision;
numMaxMoneyValue.Precision = iPrecision;
}




function onStateChange()
{
if (_iNextStatusValue != undefined)
{
var iNewState = Number(selState.DataValue);
if (_iCurrentState >= 0)
{
_aStates[_iCurrentState] = ledtStatusValues.DataXml;
_iNextStatusValue = ledtStatusValues.NextValue;
}
ledtStatusValues.DataXml = _aStates[iNewState];
ledtStatusValues.NextValue = _iNextStatusValue;
_iCurrentState = iNewState;

}

}




function hideFormatSpecificParams(oVisibleParams)
{
if (oVisibleParams != null)
{
if (oVisibleParams.length != null)
{
for (var i = 0; i < oVisibleParams.length; i++)
{
oVisibleParams(i).style.display = "none";
}
}
else
{
oVisibleParams.style.display = "none";
}
}
}

function showFormatSpecificParams(sFormat, sParamGroupSuffix)
{
var oVisibleParams = document.all(sFormat + sParamGroupSuffix);

if (oVisibleParams != null)
{
if (oVisibleParams.length != null)
{
for (var i = 0; i < oVisibleParams.length; i++)
{
oVisibleParams(i).style.display = "block";
}
}
else
{
oVisibleParams.style.display = "block";
}
}

return oVisibleParams;
}






function isFormDirty()
{
if (_bView)
{
return false;
}
return _sInputXml != getAttributeXml();
}
